package net.huashitong.scheduling.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.scheduling.entity.CoordinationIssues;
import net.huashitong.scheduling.entity.IndustrialCategory;
import net.huashitong.scheduling.entity.Project;
import net.huashitong.scheduling.entity.TownshipCondition;
import net.huashitong.scheduling.filter.ProjectFilter;
import net.huashitong.scheduling.service.CoordinationIssuesService;
import net.huashitong.scheduling.service.IndustrialCategoryService;
import net.huashitong.scheduling.service.ProjectService;
import net.huashitong.scheduling.service.TownshipConditionService;

@Controller
@RequestMapping("/app/project/")
public class ProjectController extends JsonBaseController {
    @Autowired
    private TownshipConditionService townshipConditionService;
    @Autowired
    private IndustrialCategoryService industrialCategoryService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private CoordinationIssuesService coordinationIssuesService;
    public static final String[] months = {"12", "11", "10", "09", "08", "07", "06", "05", "04", "03", "02", "01"};
    //需每年更新，不能设置太多年份，否则会降低运行速度
    public static final String[] years = {"2022", "2021","2020", "2019"};

    @Override
    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 乡镇情况
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryTownshipCondition")
    public Message queryTownshipCondition(@RequestBody ProjectFilter filter) {
        List<TownshipCondition> list = new ArrayList<TownshipCondition>();
        Map<String, Object> map = new HashMap<String, Object>();
        if (ObjectUtils.isBlank(filter.getDate())) {
            int ss = 0;
            for (int j = 0; j < ProjectController.years.length; j++) {
                if (ss == 1) {
                    break;
                }
                for (int i = 0; i < ProjectController.months.length; i++) {
                    filter.setDate(years[j] + months[i]);
                    list = townshipConditionService.queryList(filter.getDate());
                    if (ObjectUtils.isNotBlank(list)) {
                        ss = 1;
                        break;

                    }
                }
            }
        } else {
            list = townshipConditionService.queryList(filter.getDate());
        }
        map.put("lists", list);
        map.put("date", filter.getDate());
        return coverMessage("200", "数据获取成功", map);
    }

    /**
     * 乡镇情况详细
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryTownshipConditionDetail")
    public Message queryTownshipConditionDetail(@RequestBody ProjectFilter filter) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        TownshipCondition tc = townshipConditionService.queryByName(filter);
        if (ObjectUtils.isNotBlank(tc)) {
            map.put("name", tc.getName());
            map.put("intendedInvestment", tc.getIntendedInvestment());
            map.put("totalInvestmen", tc.getTotalInvestment());
            map.put("completeInvestment", tc.getCompleteInvestment());
            map.put("ratio", tc.getRatio());
            map.put("unit", tc.getUnit());
        }
        List<Project> list = projectService.queryByConstructionAddress(filter);
        if (list.size() > 0) {
            for (Project p : list) {
                Map<String, Object> mapp = new HashMap<String, Object>();
                mapp.put("sysId", p.getSysId());
                mapp.put("projectName", p.getProjectName());
                mapp.put("totalInvestment", p.getTotalInvestment());
                mapp.put("ratio", p.getRatio());
                mapp.put("currentYearInvestment", p.getCurrentYearInvestment());
                mapp.put("finishInvestment", p.getFinishInvestment());
                lists.add(mapp);
            }
        }
        map.put("lists", lists);
        return coverMessage("200", "数据获取成功", map);
    }


    /**
     * 产业类别
     */
    @ResponseBody
    @RequestMapping("/queryIndustrialCategory")
    public Message queryIndustrialCategory(@RequestBody ProjectFilter filter) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        List<IndustrialCategory> list = new ArrayList<IndustrialCategory>();

        if (ObjectUtils.isBlank(filter.getDate())) {
            int ss = 0;
            for (int j = 0; j < ProjectController.years.length; j++) {
                if (ss == 1) {
                    break;
                }
                for (int i = 0; i < ProjectController.months.length; i++) {
                    filter.setDate(years[j] + months[i]);
                    list = industrialCategoryService.queryIndustrialCategoryByDate(filter);
                    if (ObjectUtils.isNotBlank(list)) {
                        ss = 1;
                        break;

                    }
                }
            }
        } else {
            list = industrialCategoryService.queryIndustrialCategoryByDate(filter);
        }
        if (list.size() > 0) {
            for (IndustrialCategory ic : list) {
                if ("产业类别".equals(ic.getName())) {
                    map.put("name", ic.getName());
                    map.put("totalInvestment", ic.getTotalInvestment());
                    map.put("number", ic.getNumber());
                    map.put("lastYearInvestment", ic.getLastYearInvestment());
                    map.put("currentYearInvestment", ic.getCurrentYearInvestment());
                    map.put("unit", ic.getUnit());
                    map.put("nameType", ic.getNameType());
                } else {
                    Map<String, Object> mapp = new HashMap<>();
                    mapp.put("name", ic.getName());
                    mapp.put("totalInvestment", ic.getTotalInvestment());
                    mapp.put("number", ic.getNumber());
                    mapp.put("lastYearInvestment", ic.getLastYearInvestment());
                    mapp.put("currentYearInvestment", ic.getCurrentYearInvestment());
                    mapp.put("unit", ic.getUnit());
                    mapp.put("nameType", ic.getNameType());
                    lists.add(mapp);
                }
            }
        }
        map.put("lists", lists);
        map.put("date", filter.getDate());
        return coverMessage("200", "数据获取成功", map);
    }

    /**
     * 项目详细
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryProjectDetail")
    public Message queryProjectDetail(@RequestBody ProjectFilter filter) {
        if ("0".equals(filter.getType())) {
            Map<String, Object> map = new HashMap<String, Object>();
            List<Project> lists = new ArrayList<Project>();
            if (ObjectUtils.isBlank(filter.getDate())) {
                int ss = 0;
                for (int j = 0; j < ProjectController.years.length; j++) {
                    if (ss == 1) {
                        break;
                    }
                    for (int i = 0; i < ProjectController.months.length; i++) {
                        filter.setDate(years[j] + months[i]);
                        lists = projectService.queryByType(filter);
                        if (ObjectUtils.isNotBlank(lists)) {
                            ss = 1;
                            break;
                        }
                    }

                }
            } else {
                lists = projectService.queryByType(filter);
            }
            map.put("lists", lists);
            map.put("date", filter.getDate());
            return coverMessage("200", "数据获取成功", map);
        } else if ("1".equals(filter.getType())) {
            Project p = projectService.getById(filter.getNid());
            if(StringUtils.isBlank(p.getQuestions())){
                //存在问题
                p.setQuestions("-");
            }
            if(StringUtils.isBlank(p.getProposal())){
                //对策与建议
                p.setProposal("-");
            }
            if("跨乡镇".equals(p.getConstructionAddress())){
                //对策与建议
                p.setConstructionAddress(p.getResDept());
            }
            return coverMessage("200", "数据获取成功", p);
        }
        return coverMessage("200", "数据获取成功", null);
    }

    /**
     * 协调问题列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryCoordinationIssuesList")
    public Message queryCoordinationIssuesList(@RequestBody ProjectFilter filter) {
        List<CoordinationIssues> list = coordinationIssuesService.queryCoordinationIssuesByStatus(filter);
        for (CoordinationIssues cd : list) {
            cd.setLinkedLeadership("————");
        }
        return coverMessage("200", "数据获取成功", list);
    }

    /**
     * 协调问题详细
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryCoordinationIssuesListDetail")
    public Message queryCoordinationIssuesDetail(@RequestBody ProjectFilter filter) {
        CoordinationIssues ci = coordinationIssuesService.getById(filter.getNid());
        return coverMessage("200", "数据获取成功", ci);
    }

    //模糊查询(项目和挂钩领导)
    @ResponseBody
    @RequestMapping("/queryDimProject")
    public Message queryDimProject(@RequestBody ProjectFilter filter) {
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        //项目模糊查询
        if ("0".equals(filter.getType())) {
            List<Project> pList = projectService.queryByProjectName(filter);
            if (pList.size() > 0) {
                for (Project p : pList) {
                    Map<String, Object> maps = new HashMap<>();
                    maps.put("sysId", p.getSysId());
                    maps.put("projectName", p.getProjectName());
                    maps.put("totalInvestment", p.getTotalInvestment());
                    maps.put("ratio", p.getRatio());
                    maps.put("currentYearInvestment", p.getCurrentYearInvestment());
                    maps.put("finishInvestment", p.getFinishInvestment());
                    maps.put("unit", p.getUnit());
                    lists.add(maps);
                }
            }
            return coverMessage("200", "数据获取成功", lists);
            //挂钩领导查询

        } else if ("1".equals(filter.getType())) {
            String date = projectService.getLastProjectDate();
            filter.setDate(date);
            List<Project> pList = projectService.queryByLinkedLeadership(filter);
            if (pList.size() > 0) {
                for (Project p : pList) {
                    if (StringUtils.isNotBlank(p.getLinkedLeadership())) {
                        String[] ps = p.getLinkedLeadership().split("、");
                        for (String s : ps) {
                            if("-".equals(s)||s.contains(" ")){
                                continue;
                            }
                            Map<String, Object> maps = new HashMap<>();
                            maps.put("sysId", p.getSysId());
                            maps.put("name", s);
                            maps.put("type", filter.getType());
                            lists.add(maps);
                        }
                    }
                }
                int len = lists.size();
                for (int i = 0; i < len - 1; i++) {
                    Map<String, Object> temp = lists.get(i);
                    String t = (String) temp.get("name");
                    for (int j = i + 1; j < len; j++) {
                        String x = (String) lists.get(j).get("name");
                        if(t.equals(x)){
                            lists.remove(j);
                            j-- ;
                            len-- ;
                        }
                    }
                }
            }

            return coverMessage("200", "数据获取成功", lists);
        }
        return null;
    }

    @ResponseBody
    @RequestMapping("/queryProjectBylinkedLeadership")
    public Message queryProjectBylinkedLeadership(@RequestBody ProjectFilter filter) {
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        List<Project> pList = projectService.queryByLinkedLeadershipName(filter);
        if (pList.size() > 0) {
            for (Project p : pList) {
                Map<String, Object> maps = new HashMap<>();
                maps.put("sysId", p.getSysId());
                maps.put("projectName", p.getProjectName());
                maps.put("totalInvestment", p.getTotalInvestment());
                maps.put("ratio", p.getRatio());
                maps.put("currentYearInvestment", p.getCurrentYearInvestment());
                maps.put("finishInvestment", p.getFinishInvestment());
                maps.put("unit", p.getUnit());
                lists.add(maps);
            }
        }
        return coverMessage("200", "数据获取成功", lists);
    }

}
